function can(callback, defaultValue){
    try{
        return callback()
    } catch ( e ){
        return defaultValue || false;
    }
}
function usp(obj) {
    let ret = new URLSearchParams();
    for (let name in obj) {
        let value = obj[name];
        for (let i = 1; i < arguments.length; i += 2) {
            if (new RegExp(arguments[i]).test(name)) {
                if (typeof arguments[i + 1] == "function") {
                    value = arguments[i + 1](value,obj,name);
                } else {
                    value = arguments[i + 1];
                }
                break;
            }
        }
        if(value){
            ret.append(name, value);
        }
    }
    return ret;
}

Date.prototype.format = function(fmt) {
    var o = {
        "M+": this.getMonth() + 1, // 月份
        "d+": this.getDate(), // 日
        "h+": this.getHours(), // 小时
        "m+": this.getMinutes(), // 分
        "s+": this.getSeconds(), // 秒
        "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
        "S": this.getMilliseconds() // 毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

function parseDate(date, defaultValue) {
    if(!date){
        return null || defaultValue;
    } else if (date.constructor.name == 'Date') {
        return date;
    } else if (typeof (date) == 'number') {
        return new Date(date);
    } else if (typeof (date) == 'string') {
        //2021-01-31T03:22:58.000+00:00
        var regex = /(\d{4})\D+(\d{2})\D+(\d{2})\D+(\d{2})\D+(\d{2})\D+(\d{2})\.*(\d*).*/;
        if (regex.exec(date)) {
            return new Date(parseInt(RegExp.$1),
                parseInt(RegExp.$2) - 1,
                parseInt(RegExp.$3)||0,
                parseInt(RegExp.$4)||0,
                parseInt(RegExp.$5)||0,
                parseInt(RegExp.$6)||0,
                parseInt(RegExp.$7)||0);
        }
    }
    return defaultValue;
}

function formatDate(datestring, fmt) {
    var date = parseDate(datestring);
    if (date != null) {
        return date.format(fmt || 'yyyy-MM-dd hh:mm:ss');
    } else {
        return '';
    }
}

function getCookie(name) {
    let reg = new RegExp(".*\\b" + name + "\\b=([\\w\\-]+).*");
    if (reg.test(document.cookie)) {
        return document.cookie.replace(reg, "$1");
    } else {
        return "";
    }
}

function copy(content) {
    if(!content)return;
    var aux = document.getElementById("$aux$");
    //var pux = document.getElementById("$pux$");
    if(!aux){
        aux = document.createElement("textarea");
        aux.setAttribute("id", "$aux$");
        aux.style.position='fixed';
        aux.style.left='-1000px'
        document.body.appendChild(aux);
        //pux = document.createElement("script");
        //pux.setAttribute("type","text/plain")
        //pux.setAttribute("id", "$pux$");
        //pux.style.display='none';
        //document.body.appendChild(pux);
    }
    if(content.substring(0,1)=='#'){
        content = content.substring(1);
        content = document.getElementById(content);
        content = content ? content.innerText : "";
    }
    // 为了保留换行, 引入了 pux
    //pux.innerHTML = content;
    aux.innerHTML = content; //pux.innerHTML;
    aux.select();
    document.execCommand("copy");
}

function clone(src, dest){
    if(arguments.length>2){
        if(arguments[2]=="-"){
            clone(src, dest);
            for(let i=3; i<arguments.length;i++){
                delete dest[arguments[i]];
            }
        } else {
            for(let i=2; i<arguments.length;i++){
                dest[arguments[i]] = src[arguments[i]];
            }
        }
    } else {
        for( let f in src){
            dest[f] = src[f];
        }
    }
    return dest;
}

function fmtSize(size){
    size = parseInt(size);
    if(size){
        let g = Math.floor(size / (1024*1024*1024));
        size = size % (1024*1024*1024);
        let m = Math.floor(size / (1024*1024));
        size = size % (1024*1024);
        let k = Math.floor(size / (1024));
        let b = size % (1024);
        if(g>0) return g + " GB";
        if(m>0) return m + " MB";
        if(k>0) return k + " KB";
        return b + " B";
    }
    return "0字节"
}